package com.jeff.tianti.cms.dao;

import com.jeff.tianti.cms.dto.SearchKewWordDto;
import com.jeff.tianti.common.PageBean;
import com.jeff.tianti.common.dao.CustomBaseSqlDaoImpl;
import com.jeff.tianti.common.entity.PageModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeff.tianti.cms.entity.AssociationDocument;
import com.jeff.tianti.cms.dto.AssociationDocumentQueryDTO;
import org.apache.commons.lang3.StringUtils;

/**
 * @author NOW_GO
 * @desc AssociationDocumentDaoImpl类
 * @date 2019-07-19
 */

public class AssociationDocumentDaoImpl extends CustomBaseSqlDaoImpl implements AssociationDocumentDaoCustom {

    public PageModel<AssociationDocument> queryAssociationDocumentPage(AssociationDocumentQueryDTO associationDocumentQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t.title,t.id from AssociationDocument t where 1=1 and t.deleteFlag=0");
        if (associationDocumentQueryDTO != null) {
            if (StringUtils.isNotBlank(associationDocumentQueryDTO.getTitle())) {
                hql.append(" and t.title like :title ");
                map.put("title", "%" + associationDocumentQueryDTO.getTitle() + "%");
            }
        }
        hql.append(" order by t.sort desc,t.createDate desc ");
        return this.queryForPageWithParams(hql.toString(), map, associationDocumentQueryDTO.getCurrentPage(), associationDocumentQueryDTO.getPageSize());
    }

    public List<AssociationDocument> queryAssociationDocumentList(AssociationDocumentQueryDTO associationDocumentQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from AssociationDocument t ");
        return this.queryByMapParams(hql.toString(), map);
    }

    @Override
    public PageModel<Map<String, Object>> getByKewWord(PageBean pageBean) {
        StringBuilder hql = new StringBuilder();
        hql.append(" SELECT id,title ,DATE_FORMAT(create_date,'%Y-%m-%d') as createDate from association_documents WHERE delete_flag=0  order BY sort desc,create_date ");
        return this.queryForPageBySql(hql.toString(), pageBean.getCurrentPage() + 1, pageBean.getPageSize());
    }

    @Override
    public List<Map<String, Object>> listTitleByKewWordAndId(String kewWord, String id) {
        StringBuilder hql = new StringBuilder();
        hql.append("SELECT id,title,date_format(create_date, '%Y-%m-%d') AS createDate ");
        if (!StringUtils.isEmpty(id)) {
            hql.append(",content");
        }
        hql.append("from association_documents WHERE delete_flag=0 ");
        if (!StringUtils.isEmpty(kewWord)) {
            hql.append(" and title like '" + '%' + kewWord + '%' + "'");
        }
        if (!StringUtils.isEmpty(id)) {
            hql.append(" and id = '" + id+"'");
        }
        hql.append(" order BY create_date limit 0,10 ");
        return this.querySqlObjects(hql.toString());
    }
}